package indi.zjh.dp.type;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class GList<E> extends ArrayList<E> implements GTrace<E>, Serializable {
	protected HashSet<E> newset = new HashSet<E>();
	protected HashSet<E> rmset = new HashSet<E>();
	
	public boolean add(E e)
	{
		this.newset.add(e);
		return super.add(e);
	}
	
	public void add(int index, E e)
	{
		this.newset.add(e);
		super.add(index, e);
	}
	
	public boolean addAll(Collection<? extends E> c)
	{
		this.newset.addAll(c);
		return super.addAll(c);
	}
	
	public boolean addAll(int index, Collection<? extends E> c)
	{
		this.newset.addAll(c);
		return super.addAll(index, c);
	}
	
	public void clear()
	{
		Iterator<E> iter = this.iterator();
		while(iter.hasNext())
			this.rmset.add(iter.next());
		super.clear();
	}
	
	public E remove(int index)
	{
		E e = super.remove(index);
		this.rmset.add(e);
		return e;
	}
	
	public boolean remove(Object o)
	{
		this.rmset.add((E)o);
		return super.remove(o);
	}
	
	public boolean removeAll(Collection<?> c)
	{
		Iterator<?> iter = c.iterator();
		while(iter.hasNext())
			this.rmset.add((E)iter.next());
		return super.removeAll(c);
	}
	
	public E set(int index, E element)
	{
		return null;
	}
	
	/**
	 * 获取真正添加的元素集合
	 * @return
	 */
	public Set<E> getFinalAddSet()
	{
		this.newset.removeAll(this.rmset);
		return this.newset;
	}
	
	/**
	 * 获取真正移除的元素集合
	 * @return
	 */
	public Set<E> getFinalRemoveSet()
	{
		this.rmset.removeAll(this.newset);
		return this.rmset;
	}
	
	/**
	 * 清空两个临时集合
	 */
	public void clearFinalSet()
	{
		this.newset.clear();
		this.rmset.clear();
	}
}
